package org.example.myerp.service.serviceimpl;

import com.alibaba.nacos.client.utils.StringUtils;
import org.example.myerp.mapper.ModuleMapper;

import org.example.myerp.mapper.RoleMapper;
import org.example.myerp.pojo.*;
import org.example.myerp.service.ModuleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ModuleServiceImpl implements ModuleService {

	@Autowired
	private ModuleMapper moduleMapper;



	@Override
	public List<Tree> getModule(String moduleName, Integer uid) {
		return getMenus(moduleName, uid);
	}


	public List<Tree> getMenus(String moduleName, Integer uid) {
		List<Menu> menu = moduleMapper.getModule(uid);

        System.out.println(menu);
		Integer moduleMid = moduleMapper.getModuleMid(moduleName);

        System.err.println(moduleMid);
        int count = moduleMid;

		List<Tree> menuList = new ArrayList<Tree>();

		for (Menu m : menu) {
			if (m.getP_mid() == count) {
				Tree t = new Tree();
				t.setMid(m.getMid());
				t.setLabel(m.getMname());
				t.setPid(m.getP_mid());
				t.setHerf(m.getPath());
				menuList.add(t);
                System.out.println(m);
			}
		}

		for (Tree children: menuList) {
			// ��Ӷ�ݹ鷽��
          //  children.setChildren(getChild(children.getMid(), menu));
           children.setChildren(iterateMenus(menu, children.getMid()));
		}
		return menuList;
	}

	private List<Tree> getChild(Integer id, List<Menu> rootMenu) {
		// ׼�������Ӳ˵�
		List<Tree> childList = new ArrayList<Tree>();
		// �������в˵�
		for (Menu m : rootMenu) {
			Tree t = new Tree();
			t.setMid(m.getMid());
			t.setLabel(m.getMname());
			t.setPid(m.getP_mid());
			t.setHerf(m.getPath());
			if (m.getP_mid() != 0 && m.getP_mid() == id)
				childList.add(t);
		}
		// ��childList ����Ӳ˵���ѭ��һ��
		for (Tree tree : childList) {
			if (tree.getHerf() == null || tree.getHerf().equals("")) {
				// �ݹ�(�ٴε��÷���, �Ըò˵�idΪ����.�ٴκ����в˵����б����Ƚ�.�ҳ������Ӳ˵�)
				tree.setChildren(getChild(tree.getMid(), rootMenu));
			}
		}
		// �ݹ��˳�����: ���childList.size() == 0,��������ݽ����Ĳ˵���û���Ӳ˵�.
		if (childList.size() == 0) {
			return null;
		}
		return childList;
	}


    /**
     * 多级菜单查询方法
     *
     * @param menuVoList 不包含最高层次菜单的菜单集合
     * @param pid        父类id
     * @return
     */
    public List<Tree> iterateMenus(List<Menu> menuVoList, Integer pid) {
        List<Tree> result = new ArrayList<Tree>();
        for (Menu menu : menuVoList) {
            //获取菜单的id
            Integer menuid = menu.getMid();
            //获取菜单的父id
            Integer parentid = menu.getP_mid();

            if (StringUtils.isNotBlank(String.valueOf(parentid))) {
                if (parentid.equals(pid)) {
                    //递归查询当前子菜单的子菜单

                    List<Tree> iterateMenu = iterateMenus(menuVoList, menuid);
                    Tree t = new Tree();
                    t.setMid(menu.getMid());
                    t.setLabel(menu.getMname());
                    t.setPid(menu.getP_mid());
                    t.setHerf(menu.getPath());
                    t.setChildren(iterateMenu);
                    result.add(t);
                }
            }
        }
        return result;
    }

}
